package org.dromara.system.controller.business;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.CommissionImportVo;
import org.dromara.common.excel.core.ExcelResult;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.CommissionVo;
import org.dromara.system.domain.bo.CommissionBo;
import org.dromara.system.service.ICommissionService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 佣金信息
 *
 * @author joker
 * @date 2025-05-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/commission")
public class CommissionController extends BaseController {

    private final ICommissionService commissionService;

    /**
     * 查询佣金信息列表
     */
    @SaCheckPermission("system:commission:list")
    @GetMapping("/list")
    public TableDataInfo<CommissionVo> list(CommissionBo bo, PageQuery pageQuery) {
        return commissionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出佣金信息列表
     */
    @SaCheckPermission("system:commission:export")
    @Log(title = "佣金信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommissionBo bo, HttpServletResponse response) {
        List<CommissionVo> list = commissionService.queryList(bo);
        ExcelUtil.exportExcel(list, "佣金信息", CommissionVo.class, response);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "佣金信息数据", CommissionImportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "佣金信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:commission:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<CommissionImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), CommissionImportVo.class, true);
        return toAjax(commissionService.insertList(excelResult.getList()));
    }

    /**
     * 获取佣金信息详细信息
     *
     * @param commissionId 主键
     */
    @SaCheckPermission("system:commission:query")
    @GetMapping("/{commissionId}")
    public R<CommissionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long commissionId) {
        return R.ok(commissionService.queryById(commissionId));
    }

    /**
     * 新增佣金信息
     */
    @SaCheckPermission("system:commission:add")
    @Log(title = "佣金信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommissionBo bo) {
        return toAjax(commissionService.insertByBo(bo));
    }

    /**
     * 修改佣金信息
     */
    @SaCheckPermission("system:commission:edit")
    @Log(title = "佣金信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommissionBo bo) {
        return toAjax(commissionService.updateByBo(bo));
    }

    /**
     * 删除佣金信息
     *
     * @param commissionIds 主键串
     */
    @SaCheckPermission("system:commission:remove")
    @Log(title = "佣金信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commissionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] commissionIds) {
        return toAjax(commissionService.deleteWithValidByIds(List.of(commissionIds), true));
    }
}
